from forest.plugins import PluginRegistry, search_plugins, PluginMeta, registry
TOOLS_REGISTRY_PREFIX = 'tools.'

command_registry = {}

def get_tools():
    ''' Returns a dictionary of all registered tools. '''
    tools = []
    for k,v in registry.iteritems():
        if k.startswith(TOOLS_REGISTRY_PREFIX):
            tools.append((k, v))
            
    return dict(tools)
    
def get_tool(name):
    ''' Returns a tool defined by name.'''
    return registry.get(TOOLS_REGISTRY_PREFIX+name)    
    
class register_tool(object):
    ''' This is a decorator intended to allow registration of tools plugins
        under tools. in the global plugin registry
        
    '''
    def __init__(self, name):
        """
        If there are decorator arguments, the function
        to be decorated is not passed to the constructor!
        """
        self.name = name

    def __call__(self, f_or_class):
        """
        If there are decorator arguments, __call__() is only called
        once, as part of the decoration process! You can only give
        it a single argument, which is the function object.
        """
        key = TOOLS_REGISTRY_PREFIX+self.name
        registry.register(key, f_or_class)
        f_or_class.__register__ = key 
        return f_or_class
